
var host = "/";

var seriesdata = [];
var pcdata = [];
var appdata = [];
var Organizationdata = [];

$(function () {

    //获取所有支部的人数
    GetALLOrganization();

    var date = new Date();
    var month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1);
    var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    var nowDate = date.getFullYear() + "-" + month + "-" + day;
    $("#selectDate").val(nowDate);
    var title = $("#txtTitle").val();
    Statistics(1, nowDate, title);//默认显示在线人数
    $("#selectType").change(function () {
        var val = $(this).val();
        Statistics(val, $("#selectDate").val(), $("#txtTitle").val());
    });
    Calendar.prototype.complete = function () {
        var val = $("#selectType").val();
        Statistics(val, $("#selectDate").val(), $("#txtTitle").val());
    }

    $("#btnSearch").click(function () {
        var val = $("#selectType").val();
        Statistics(val, $("#selectDate").val(), $("#txtTitle").val());
    });

    //统计结果汇总
    StatisticsSummary();

});


//党建平台统计
function Statistics(type, datetime, title) {
    $.ajax({
        type: "POST",
        dataType: "text",
        url: host + "WS/CityOnLineCount.ashx",
        data: { type: type, datetime: datetime, title: title },
        success: function (data) {
            var result = eval("(" + data + ")");
            seriesdata = eval(result.Data);
            pcdata = eval(result.PCData.split(","));
            appdata = eval(result.APPData.split(","));
            var maxcount = 10;
            var scrollbarEnabled = true;
            var oldcount = seriesdata.length;
            if (oldcount < 10) {
                maxcount = oldcount - 1;
                scrollbarEnabled = false;
            }

            $('#onlinecount').highcharts({
                title: {
                    text: '',
                    x: -20 //center
                },
                tooltip: {
                    allowDecimals: false
                },
                chart: { type: 'column' },
                subtitle: {
                    text: '',
                    x: -20
                },
                xAxis: {
                    type: 'category',
                    min: 0,
                    max: maxcount
                },
                scrollbar: {
                    enabled: scrollbarEnabled
                },
                yAxis: {
                    allowDecimals: false,
                    min: 0,
                    title: {
                        text: '人数 (个)'
                    },
                    plotLines: [
                        {
                            value: 0,
                            width: 1,
                            color: '#808080'
                        }
                    ]
                },
                colors: ['#EA0000', '#0080FF'],
                tooltip: {
                    shared: true,
                    formatter: function () {
                        var val = seriesdata[this.x][0];
                        var sum = 0;
                        for (var i = 0; i < Organizationdata.length; i++) {
                            if (Organizationdata[i][0] == val) {
                                sum = Organizationdata[i][1];
                                break;
                            }
                        }

                        sum = parseInt(pcdata[this.x]) + parseInt(appdata[this.x]);

                        //return seriesdata[this.x][0] + '总人数：' + sum + '<br/>平台：' + pcdata[this.x] + '次<br/>APP：' + appdata[this.x] + '次';
                        //演示用的假数据
                        return seriesdata[this.x][0] + '总人数：' + sum + '<br/>平台：' + pcdata[this.x] + '次<br/>APP：' + appdata[this.x] + '次';
                    }
                },
                legend: {
                    layout: 'vertical',
                    align: 'center',
                    verticalAlign: 'bottom',
                    borderWidth: 0
                },
                credits: {
                    enabled: false
                },
                background: '#E0E0E8',
                series: [{
                    name: ' ',
                    data: seriesdata,
                    dataLabels: {
                        enabled: true,
                        color: '#FFFFFF',
                        align: 'center',
                        y: 25,
                        style: {
                            fontSize: '13px',
                            fontFamily: 'Verdana, sans-serif'
                        }
                    }
                }]
            });

            $(".highcharts-legend-item").remove();
        },
        error: function (errormsg) {
        }
    });
}

//获取所有支部人数
function GetALLOrganization() {
    $.ajax({
        type: "POST",
        dataType: "text",
        url: host + "WS/CityOnLineCount.ashx",
        data: { type: 3 },
        success: function (data) {
            Organizationdata = eval(data);
        },
        error: function (errormsg) {
        }
    });
}

//统计结果汇总
function StatisticsSummary() {
    $.ajax({
        type: "POST",
        dataType: "text",
        url: host + "WS/CityOnLineCount.ashx",
        data: { type: 4 },
        success: function (data) {
            $("#SSummary").html(data);
        },
        error: function (errormsg) {
        }
    });
}
